package com.mc.developmentkit.share;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.mc.developmentkit.bean.ShareInfo;
import com.mc.developmentkit.callback.ShareListener;

import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by LeBron on 2016/7/13.
 * 分享给社交平台
 */
public class MCThirdPartyShare {


    private static MCThirdPartyShare instance;

    private MCThirdPartyShare() {
    }

    public static MCThirdPartyShare getInstance() {
        if (null == instance) {
            instance = new MCThirdPartyShare();
        }
        return instance;
    }

    public void showShare(Context context, ShareInfo shareInfo, List<String> list, ShareListener shareListener) {
        this.shareListener=shareListener;
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle(shareInfo.title);
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(shareInfo.shareUrl);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(shareInfo.text);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(shareInfo.shareUrl);
        //imageUrl问过share客服,QQ微信都能用
        oks.setImageUrl(shareInfo.imgUrl);
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(shareInfo.appName);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(shareInfo.shareUrl);
        //自定义回调
        if (shareListener != null) {
            oks.setCallback(platformActionListener);
        }
        //隐藏部分不需要的分享平台
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                oks.addHiddenPlatform(list.get(i));
            }
        }
        // 启动分享GUI
        oks.show(context);
    }

    private static final int MSG_TOAST = 1;
    private static final int MSG_ACTION_CALLBACK = 2;
    private static final int MSG_CANCEL_NOTIFY = 3;

    private ShareListener shareListener;
    private String msg1;
    private String code;

    PlatformActionListener platformActionListener=new PlatformActionListener() {

        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            // 成功
            Message msg = new Message();
            msg.what = MSG_TOAST;
            sHandler.sendMessage(msg);
        }
        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            // 失敗
            //打印错误信息,print the error msg
            throwable.printStackTrace();
            //错误监听,handle the error msg
            Message msg = new Message();
            msg.what = MSG_ACTION_CALLBACK;
            sHandler.sendMessage(msg);

        }
        @Override
        public void onCancel(Platform platform, int i) {
            // 取消
            Message msg = new Message();
            msg.what = MSG_CANCEL_NOTIFY;
            sHandler.sendMessage(msg);
        }
    };
    /**
     * 分享回调Handler
     */
     Handler sHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_TOAST:
                    msg1 = "分享成功";
                    code=MSG_TOAST+"";
                    break;
                case MSG_ACTION_CALLBACK:
                    msg1 = "分享失败";
                    code=MSG_ACTION_CALLBACK+"";
                    break;
                case MSG_CANCEL_NOTIFY:
                    msg1 = "取消分享";
                    code=MSG_CANCEL_NOTIFY+"";
                    break;
            }
            shareListener.shareResult("{\"msg\":\""  + msg1 + "\",\"code\":" + code+ "}");
        }
    };
}
